﻿using System;
using System.Collections.Generic;
using System.Data;
using System.Web;
using System.Text;
using System.Data.SqlClient;
using DeviceInfoManage.DataAccessLayer;

namespace DeviceInfoManage.BusinessLogicLayer
{
    /// <summary>
    /// maintRecord
    /// </summary>
    [Serializable]
    public class maintRecordInfo
    {
        #region 字段属性
        private int _recordid;
        private string _deviceuser;
        private string _departmentname;
        private DateTime _mainttime;
        private DateTime _comtime;
        private string _errtype;
        private string _devicemodel;
        private string _deviceserialnumber;
        private string _propertyserialnumber;
        private int _maintpersonid;
        private string _errReason;
        private string _errSolveWay;
        private string _state;
        private string _issolve;
        /// <summary>
        /// 维护记录ID
        /// </summary>
        public int recordId
        {
            set { _recordid = value; }
            get { return _recordid; }
        }
        /// <summary>
        /// 设备使用人
        /// </summary>
        public string deviceUser
        {
            set { _deviceuser = value; }
            get { return _deviceuser; }
        }
        /// <summary>
        /// 所属部门
        /// </summary>
        public string departmentName
        {
            set { _departmentname = value; }
            get { return _departmentname; }
        }
        /// <summary>
        /// 到达时间
        /// </summary>
        public DateTime maintTime
        {
            set { _mainttime = value; }
            get { return _mainttime; }
        }
        /// <summary>
        /// 完成时间
        /// </summary>
        public DateTime ComTime
        {
            set { _comtime = value; }
            get { return _comtime; }
        }
        /// <summary>
        /// 故障类型
        /// </summary>
        public string errType
        {
            set { _errtype = value; }
            get { return _errtype; }
        }
        /// <summary>
        /// 设备型号
        /// </summary>
        public string deviceModel
        {
            set { _devicemodel = value; }
            get { return _devicemodel; }
        }
        /// <summary>
        /// 设备编号
        /// </summary>
        public string deviceSerialNumber
        {
            set { _deviceserialnumber = value; }
            get { return _deviceserialnumber; }
        }
        /// <summary>
        /// 资产编号
        /// </summary>
        public string propertySerialNumber
        {
            set { _propertyserialnumber = value; }
            get { return _propertyserialnumber; }
        }
        /// <summary>
        /// 维护人
        /// </summary>
        public int maintPersonId
        {
            set { _maintpersonid = value; }
            get { return _maintpersonid; }
        }
        /// <summary>
        /// 故障原因
        /// </summary>
        public string errReason
        {
            get { return _errReason; }
            set { _errReason = value; }
        }
        /// <summary>
        /// 故障解决方法
        /// </summary>
        public string errSolveWay
        {
            set { _errSolveWay = value; }
            get { return _errSolveWay; }
        }
        /// <summary>
        /// 单据状态
        /// </summary>
        public string State
        {
            get { return _state; }
            set { _state = value; }
        }
        /// <summary>
        /// 故障是否解决
        /// </summary>
        public string IsSolve
        {
            get { return _issolve; }
            set { _issolve = value; }
        }
        #endregion

        #region 构造函数
        public maintRecordInfo()
        {

        }
        #endregion

        #region  成员方法

        /// <summary>
        /// 得到最大ID
        /// </summary>
        public int GetMaxId()
        {
            return DbHelperSQL.GetMaxID("recordId", "maintRecord");
        }

        /// <summary>
        /// 是否存在该记录
        /// </summary>
        public bool Exists(int recordId)
        {
            StringBuilder strSql = new StringBuilder();
            strSql.Append("select count(1) from maintRecord");
            strSql.Append(" where recordId=@recordId ");
            SqlParameter[] parameters = {
					new SqlParameter("@recordId", SqlDbType.Int,4)};
            parameters[0].Value = recordId;

            return DbHelperSQL.Exists(strSql.ToString(), parameters);
        }


        /// <summary>
        /// 增加一条数据
        /// </summary>
        public int Add(maintRecordInfo model)
        {
            StringBuilder strSql = new StringBuilder();
            strSql.Append("insert into maintRecord(");
            strSql.Append("deviceUser,departmentName,maintTime,ComTime,errType,deviceModel,deviceSerialNumber,propertySerialNumber,maintPersonId,errReason,errSolveWay,State,IsSolve)");
            strSql.Append(" values (");
            strSql.Append("@deviceUser,@departmentName,@maintTime,@ComTime,@errType,@deviceModel,@deviceSerialNumber,@propertySerialNumber,@maintPersonId,@errReason,@errSolveWay,@State,@IsSolve)");
            strSql.Append(";select @@IDENTITY");
            SqlParameter[] parameters = {
					new SqlParameter("@deviceUser", SqlDbType.NVarChar,20),
					new SqlParameter("@departmentName", SqlDbType.NVarChar,20),
					new SqlParameter("@maintTime", SqlDbType.DateTime),
					new SqlParameter("@ComTime", SqlDbType.DateTime),
					new SqlParameter("@errType", SqlDbType.NVarChar,20),
					new SqlParameter("@deviceModel", SqlDbType.NVarChar,50),
					new SqlParameter("@deviceSerialNumber", SqlDbType.VarChar,50),
					new SqlParameter("@propertySerialNumber", SqlDbType.VarChar,50),
					new SqlParameter("@maintPersonId", SqlDbType.Int,4),
					new SqlParameter("@errReason", SqlDbType.VarChar,1000),
					new SqlParameter("@errSolveWay", SqlDbType.VarChar,1000),
					new SqlParameter("@State", SqlDbType.Char,1),
					new SqlParameter("@IsSolve", SqlDbType.Char,1)};
            parameters[0].Value = model.deviceUser;
            parameters[1].Value = model.departmentName;
            parameters[2].Value = model.maintTime;
            parameters[3].Value = model.ComTime;
            parameters[4].Value = model.errType;
            parameters[5].Value = model.deviceModel;
            parameters[6].Value = model.deviceSerialNumber;
            parameters[7].Value = model.propertySerialNumber;
            parameters[8].Value = model.maintPersonId;
            parameters[9].Value = model.errReason;
            parameters[10].Value = model.errSolveWay;
            parameters[11].Value = model.State;
            parameters[12].Value = model.IsSolve;

            object obj = DbHelperSQL.GetSingle(strSql.ToString(), parameters);
            if (obj == null)
            {
                return 1;
            }
            else
            {
                return Convert.ToInt32(obj);
            }
        }
        /// <summary>
        /// 更新一条数据
        /// </summary>
        public int Update(maintRecordInfo model)
        {
            StringBuilder strSql = new StringBuilder();
            strSql.Append("update maintRecord set ");
            strSql.Append("deviceUser=@deviceUser,");
            strSql.Append("departmentName=@departmentName,");
            strSql.Append("maintTime=@maintTime,");
            strSql.Append("ComTime=@ComTime,");
            strSql.Append("errType=@errType,");
            strSql.Append("deviceModel=@deviceModel,");
            strSql.Append("deviceSerialNumber=@deviceSerialNumber,");
            strSql.Append("propertySerialNumber=@propertySerialNumber,");
            strSql.Append("maintPersonId=@maintPersonId,");
            strSql.Append("errReason=@errReason,");
            strSql.Append("errSolveWay=@errSolveWay,");
            strSql.Append("State=@State,");
            strSql.Append("IsSolve=@IsSolve");
            strSql.Append(" where recordId=@recordId ");
            SqlParameter[] parameters = {
					new SqlParameter("@recordId", SqlDbType.Int,4),
					new SqlParameter("@deviceUser", SqlDbType.NVarChar,20),
					new SqlParameter("@departmentName", SqlDbType.NVarChar,20),
					new SqlParameter("@maintTime", SqlDbType.DateTime),
					new SqlParameter("@ComTime", SqlDbType.DateTime),
					new SqlParameter("@errType", SqlDbType.NVarChar,20),
					new SqlParameter("@deviceModel", SqlDbType.NVarChar,50),
					new SqlParameter("@deviceSerialNumber", SqlDbType.VarChar,50),
					new SqlParameter("@propertySerialNumber", SqlDbType.VarChar,50),
					new SqlParameter("@maintPersonId", SqlDbType.Int,4),
					new SqlParameter("@errReason", SqlDbType.VarChar,1000),
					new SqlParameter("@errSolveWay", SqlDbType.VarChar,1000),
					new SqlParameter("@State", SqlDbType.Char,1),
					new SqlParameter("@IsSolve", SqlDbType.Char,1)};
            parameters[0].Value = model.recordId;
            parameters[1].Value = model.deviceUser;
            parameters[2].Value = model.departmentName;
            parameters[3].Value = model.maintTime;
            parameters[4].Value = model.ComTime;
            parameters[5].Value = model.errType;
            parameters[6].Value = model.deviceModel;
            parameters[7].Value = model.deviceSerialNumber;
            parameters[8].Value = model.propertySerialNumber;
            parameters[9].Value = model.maintPersonId;
            parameters[10].Value = model.errReason;
            parameters[11].Value = model.errSolveWay;
            parameters[12].Value = model.State;
            parameters[13].Value = model.IsSolve;

            return DbHelperSQL.ExecuteSql(strSql.ToString(), parameters);
        }

        /// <summary>
        /// 删除一条数据
        /// </summary>
        public int Delete(int recordId)
        {

            StringBuilder strSql = new StringBuilder();
            strSql.Append("delete from maintRecord ");
            strSql.Append(" where recordId=@recordId ");
            SqlParameter[] parameters = {
					new SqlParameter("@recordId", SqlDbType.Int,4)};
            parameters[0].Value = recordId;

            return DbHelperSQL.ExecuteSql(strSql.ToString(), parameters);
        }


        /// <summary>
        /// 得到一个对象实体
        /// </summary>
        public maintRecordInfo GetModel(int recordId)
        {

            StringBuilder strSql = new StringBuilder();
            strSql.Append("select  top 1 recordId,deviceUser,departmentName,maintTime,ComTime,errType,deviceModel,deviceSerialNumber,propertySerialNumber,maintPersonId,errReason,errSolveWay,State,IsSolve from maintRecord ");
            strSql.Append(" where recordId=@recordId ");
            SqlParameter[] parameters = {
					new SqlParameter("@recordId", SqlDbType.Int,4)};
            parameters[0].Value = recordId;

            maintRecordInfo model = new maintRecordInfo();
            DataSet ds = DbHelperSQL.Query(strSql.ToString(), parameters);
            if (ds.Tables[0].Rows.Count > 0)
            {
                if (ds.Tables[0].Rows[0]["recordId"].ToString() != "")
                {
                    model.recordId = int.Parse(ds.Tables[0].Rows[0]["recordId"].ToString());
                }
                model.deviceUser = ds.Tables[0].Rows[0]["deviceUser"].ToString();
                model.departmentName = ds.Tables[0].Rows[0]["departmentName"].ToString();
                if (ds.Tables[0].Rows[0]["maintTime"].ToString() != "")
                {
                    model.maintTime = DateTime.Parse(ds.Tables[0].Rows[0]["maintTime"].ToString());
                }
                if (ds.Tables[0].Rows[0]["ComTime"].ToString() != "")
                {
                    model.ComTime = DateTime.Parse(ds.Tables[0].Rows[0]["ComTime"].ToString());
                }
                model.errType = ds.Tables[0].Rows[0]["errType"].ToString();
                model.deviceModel = ds.Tables[0].Rows[0]["deviceModel"].ToString();
                model.deviceSerialNumber = ds.Tables[0].Rows[0]["deviceSerialNumber"].ToString();
                model.propertySerialNumber = ds.Tables[0].Rows[0]["propertySerialNumber"].ToString();
                if (ds.Tables[0].Rows[0]["maintPersonId"].ToString() != "")
                {
                    model.maintPersonId = int.Parse(ds.Tables[0].Rows[0]["maintPersonId"].ToString());
                }
                model.errReason = ds.Tables[0].Rows[0]["errReason"].ToString();
                model.errSolveWay = ds.Tables[0].Rows[0]["errSolveWay"].ToString();
                model.State = ds.Tables[0].Rows[0]["State"].ToString();
                model.IsSolve = ds.Tables[0].Rows[0]["IsSolve"].ToString();
                return model;
            }
            else
            {
                return null;
            }
        }

        /// <summary>
        /// 获得数据列表
        /// </summary>
        public DataSet GetList(string strWhere)
        {
            StringBuilder strSql = new StringBuilder();
            strSql.Append("select recordId,deviceUser,departmentName,maintTime,ComTime,errType,deviceModel,deviceSerialNumber,propertySerialNumber,maintPersonId,errReason,errSolveWay,State,IsSolve ");
            strSql.Append(" FROM maintRecord ");
            if (strWhere.Trim() != "")
            {
                strSql.Append(" where " + strWhere);
            }
            return DbHelperSQL.Query(strSql.ToString());
        }

        /// <summary>
        /// 获得前几行数据
        /// </summary>
        public DataSet GetList(int Top, string strWhere, string filedOrder)
        {
            StringBuilder strSql = new StringBuilder();
            strSql.Append("select ");
            if (Top > 0)
            {
                strSql.Append(" top " + Top.ToString());
            }
            strSql.Append(" recordId,deviceUser,departmentName,maintTime,ComTime,errType,deviceModel,deviceSerialNumber,propertySerialNumber,maintPersonId,errReason,errSolveWay,State,IsSolve ");
            strSql.Append(" FROM maintRecord ");
            if (strWhere.Trim() != "")
            {
                strSql.Append(" where " + strWhere);
            }
            strSql.Append(" order by " + filedOrder);
            return DbHelperSQL.Query(strSql.ToString());
        }

        /// <summary>
        /// 获得数据列表
        /// </summary>
        public DataSet GetList()
        {
            StringBuilder strSql = new StringBuilder();
            strSql.Append("select * FROM errTypeInfo ");
            return DbHelperSQL.Query(strSql.ToString());
        }

        /// <summary>
        /// 审核单据
        /// </summary>
        public void SetState(string rid)
        {
            StringBuilder strSql = new StringBuilder();
            strSql.Append("UPDATE [maintRecord] SET [State] = '1' WHERE [recordId] = '" + rid + "'");
            DbHelperSQL.GetSingle(strSql.ToString());
        }

        public DataSet QueryDevice(maintRecordInfo mr, string UserType)
        {
            try
            {
                string queryString = "select * from recordInfoView";
                string strSql = string.Empty;
                if (mr.departmentName != "") strSql = " and departmentName like '%" + mr.departmentName + "%'";
                if (mr.errType != "") strSql += " and errTypeName like '%" + mr.errType + "%'";
                if (mr.deviceModel != "") strSql += " and deviceModel like '%" + mr.deviceModel + "%'";
                if (!string.IsNullOrEmpty(mr.deviceSerialNumber)) strSql += " and deviceSerialNumber like '%" + mr.deviceSerialNumber + "%'";
                if (mr.maintTime != DateTime.MinValue) strSql += " and maintTime >  cast('" + mr.maintTime.ToString() + "' as datetime)";
                if (mr.ComTime != DateTime.MinValue) strSql += " and maintTime <  convert(datetime,'" + mr.ComTime.ToString() + "')";
                if (!string.IsNullOrEmpty(mr.State)) { strSql += " and State ='" + mr.State + "'"; }
                else
                    if (UserType != "管理员") { strSql += " and State = '1'"; }
                if (strSql.StartsWith(" and"))
                {
                    strSql = strSql.Remove(0, 4);
                    queryString += " where" + strSql;
                }
                ///*如果用户设备了开始时间的查询条件*/
                //if (startTimeString != "")
                //    queryString += " and devicePurchaseTime>'" + Convert.ToDateTime(startTimeString) + "'";
                ///*如果用户设置了结束时间的查询条件*/
                //if (endTimeString != "")
                //    queryString += " and devicePurchaseTIme<'" + Convert.ToDateTime(endTimeString) + "'";

                DataBase db = new DataBase();
                return db.GetDataSet(queryString);
            }
            catch
            {
                return null;
            }
        }

        public DataSet DeviceStatistics(string strSql)
        {
            try
            {
                //string queryString = "select Principal,Count(DISTINCT [Count]) AS Count,errTypeName from maintRecordStatistics";
                //if (strSql.StartsWith(" AND"))
                //{
                //    strSql = strSql.Remove(0, 4);
                //    queryString += " where" + strSql;
                //}
                //queryString += " group by Principal,errTypeName";
                StringBuilder queryString = new StringBuilder();
                queryString.Append("select count(A.recordid) AS [Count], B.errTypeName,C.maintPersonName AS Principal ");
                queryString.Append("from maintRecord A inner join errTypeInfo B on A.errType=B.errTypeId ");
                queryString.Append("inner join maintPersonInfo C on A.maintPersonId = C.maintPersonId ");
                if (strSql.StartsWith(" AND"))
                {
                    strSql = strSql.Remove(0, 4);
                    queryString.AppendFormat(" WHERE {0}",strSql);
                }
                queryString.Append(" group by errTypeName,maintPersonName");

                DataBase db = new DataBase();
                return db.GetDataSet(queryString.ToString());
            }
            catch
            {
                return null;
            }
        }
        #endregion  成员方法
    }
}
